package com.verio.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.verio.dao.ContractDao;
import com.verio.entity.Contract;
import com.verio.utils.Assert;
import com.verio.utils.BeanUtils;
import com.verio.utils.VariableNameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Service
public class ContractService extends BaseService<Contract>{

    @Autowired
    protected ContractDao contractDao;
    @Override
    public Contract save(Contract contract) {
        if (Assert.isEmpty(contract.getId())) {
            contractDao.insert(contract);
        } else {
            contractDao.updateById(contract);
        }
        return contractDao.selectById(contract.getId());
    }

    @Override
    public Contract get(Serializable id) {
        return contractDao.selectById(id);
    }

    @Override
    public int delete(Serializable id) {
        return contractDao.deleteById(id);
    }

    @Override
    public List<Contract> query(Contract o) {
        QueryWrapper<Contract> wrapper = new QueryWrapper<>();
        if (!Assert.notEmpty(o)) return contractDao.selectList(wrapper);
        Map<String, Object> bean2Map = BeanUtils.bean2Map(o);
        for (String key : bean2Map.keySet()) {
            if (Assert.isEmpty(bean2Map.get(key))) {
                continue;
            }
            wrapper.eq(VariableNameUtils.humpToLine(key), bean2Map.get(key));
        }

        return contractDao.selectList(wrapper);
    }

    @Override
    public List<Contract> all() {
        return query(null);
    }

    public List<Contract> getByUserId(String idcard) {
        return contractDao.getByUserId(idcard);
    }

    public Contract getContractDetail(String landlord, String tenant) {
        return contractDao.getByLandlordAndTenant(landlord, tenant);
    }
}
